import 'package:cloud_phone_manager/dialog/ScrcpyConnectDialog.dart';
import 'package:cloud_phone_manager/provider/AdbCurrentChangeProvider.dart';
import 'package:cloud_phone_manager/utils/CmdUtils.dart';
import 'package:cloud_phone_manager/widget/AppManager.dart';
import 'package:cloud_phone_manager/widget/AppProgressManager.dart';
import 'package:cloud_phone_manager/widget/HeaderTabView.dart';
import 'package:cloud_phone_manager/widget/RuichiChangeView.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../data/TabData.dart';
import '../provider/AdbDevicesProvider.dart';
import 'DevicesInfoView.dart';
import 'FileManagerView.dart';
import 'LogView.dart';

/// 右侧主体内容
class ContentFragmentView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _LogView();
}

class _LogView extends State<ContentFragmentView> {
  final List<Widget> _list = [];
  late Widget _headerTabView;
  late Widget _logView;
  bool _mounted = false;
  bool _isInit = false;
  late LogView log;
  late DevicesInfoView _devicesInfoView;
  late AppManager _appManager;
  late AppProgressManager _appProgressManager;
  late RuichiChangeView _ruichiChangeView;
  late FileManagerView _fileManagerView;
  late Widget _currentView;
  late Widget _line;
  AdbDevicesProvider? provider;

  @override
  void initState() {
    super.initState();
    _mounted = true;
    if(!_isInit){
      _isInit = true;
      log = LogView();
      _devicesInfoView = DevicesInfoView();
      _appManager = AppManager();
      _appProgressManager = AppProgressManager();
      _ruichiChangeView = RuichiChangeView();
      _fileManagerView = FileManagerView();
      _currentView = _fileManagerView;
      _headerTabView = Expanded(
          flex: 1,
          child: HeaderTabView(
            headerTabClickCallback: (TabData tab) {
              if(tab.index == 0 && _currentView != log){
                setState(() {
                  _currentView = log;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }else if(tab.index == 6 && _currentView != _devicesInfoView){
                setState(() {
                  _currentView = _devicesInfoView;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }else if(tab.index == 4 && _currentView != _appManager){
                setState(() {
                  _currentView = _appManager;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }else if(tab.index == 5 && _currentView != _appProgressManager){
                setState(() {
                  _currentView = _appProgressManager;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }else if(tab.index == 11 && _currentView != _ruichiChangeView){
                setState(() {
                  _currentView = _ruichiChangeView;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }else if(tab.index == 3 && _currentView != _fileManagerView){
                setState(() {
                  _currentView = _fileManagerView;
                  _list.remove(_logView);
                  _logView = Expanded(flex: 19, child: _currentView);
                  _list.add(_logView);
                });
              }
            },
          ));
      _line = Container(color: Color.fromARGB(15, 0, 0, 0),height: 0.5);
      _logView = Expanded(flex: 19, child: _currentView);
    }
  }

  @override
  void dispose() {
    _mounted = false;
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    if (provider == null) {
      provider = Provider.of<AdbDevicesProvider>(context);
      debugPrint("注册监听");
      provider?.addListener(() {
        if(_mounted){
          if (provider!.getAdbDevicesInfoList().isNotEmpty && _list.isEmpty) {
            debugPrint("进行ui更新");
            setState(() {
              _list.add(_headerTabView);
              _list.add(_line);
              _list.add(_logView);
            });
          } else if (provider!.getAdbDevicesInfoList().isEmpty) {
            setState(() {
              _list.clear();
            });
          }
        }
      });
    }
    /*return Container(
        child: Column(
      children: _list,
    ));*/
    return Container(
        child: Column(
          children: _list,
        ));
  }
}
